from login.page import page_get
from login.登录聚水潭 import login_聚水潭
import datetime
import time
import pandas as pd
from datetime import date
import requests

page = page_get()
login_聚水潭('18679184275', 'ft77.9o112x')
page.wait(2)
page('报表').click()
page.wait(1)
page('销售主题分析(财务)').click()
time.sleep(5)
# 勾选复选框
page('#status_waitpay').click()
page('#status_waitconfirm').click()
page('#status_waitfconfirm').click()
page('#status_waitdeliver').click()
page('#status_delivering').click()
page('#status_question').click()
page('#status_WaitOuterSent').click()
# 设置标签
page('#labels').click()
time.sleep(1)
page('#label_572613263').click()
# 确定
page('.btn_1').click()
time.sleep(2)
# 计算相差的天数
start_date = date(2024, 3, 30)
end_date = date(2024, 3, 30)
delta = (start_date - end_date).days
for i in range(1, delta + 2):
    # 输入日期
    date = (datetime.datetime.now() - datetime.timedelta(days=i)).strftime('%Y-%m-%d')
    page('#order_date_begin').input(date, clear=True)
    page('#order_date_end').input(date, clear=True)
    # 生成报表
    page('.btn_search').click()
    time.sleep(3)
    page('.excel').wait.displayed(timeout=15)
    time.sleep(2)
    trs = page.eles('.rpt body',timeout=5)
    if trs:
        tr = trs[0]
        td = tr.eles('t:td')
        # 请求的 URL
        url = 'http://192.168.16.21:30695/api/financewh/Import/InsertCompanyAmount'
        # 请求的参数
        payload = [{
            'shopName': td[1].text,
            'receptDate': date,
            'saleAmount': str(td[7].text).replace(',', ''),
            'returnAmount': str(td[21].text).replace(',', '')
        }]
        print(payload)
        # 发送 POST 请求，并使用 json 参数传递数据
        response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
        # 获取响应内容
        print(response.text)
    else:
        print(f'{date}没有数据')
page.quit()
